#!/usr/bin/python

# visualize machine active time per job 
# input: job tracker aggregate log file
# output: the active time of each machine

import sys
import re


def getvalue(a):
	return a[a.find("=")+1:].strip("\"")

def getjobid(a):
	A = a.split('_')
	return A[2]

def gethostname(a):
	return a[a.find("cloud")+5:a.find("\\")]

def savevalue(a, b, c, f):
	len = b - a
	delta = len/2
	center = a + delta
	tmp = str(center-1272500000)+' '+str(c)+' '+str(delta)+'\n'
	f.write(tmp)


if len(sys.argv) <= 1:
	print "./COMMAND [job tracker log file name]?\n"
	sys.exit(-1)

# get machine id mapping from file
n2id = {}
id2n = {}
for l in open("mapping.txt"):
	A = l.split()
	id2n[int(A[0])] = A[1]
	n2id[A[1]] = int(A[0])


hosts = {}
for l in open(sys.argv[1]):
	A = l.split()
	if A[0] in ["MapAttempt", "ReduceAttempt"]:
		hostname = {}
		id = 0
		tmp1  = 100000000000000
		tmp2  = 0
	   	for a in A:
		   	# read task hostname, translate into host id 
			if (a.startswith("HOSTNAME")) or (a.startswith("TRACKER_NAME")):
			   	hostname = gethostname(a)
				id = n2id[hostname]
	#			print str(id)+'  '+hostname

			# get time stamp 
			if a.startswith("START_TIME"):
				tmp1 = (int(getvalue(a)))/1000 

			if a.startswith("FINISH_TIME"):
				tmp2 = (int(getvalue(a)))/1000 


		# ignore invalid id
		if (id == 0) or (id > 64) :
			print "error"
			continue

		# update active time range
		if not hosts.has_key(id):
			hosts[id] = {}
			hosts[id]["start"] = 100000000000000
			hosts[id]["finish"] = 0

		if tmp1 < hosts[id]["start"]:
			hosts[id]["start"] = tmp1
		if tmp2 > hosts[id]["finish"]:
			hosts[id]["finish"] = tmp2

# save per machine average information (for plot)
mf = open('timeplot.dat', 'w')
for k,v in hosts.items():
	savevalue (v["start"], v["finish"], k, mf)
mf.close()


# save per machine average information
mf = open('time.txt', 'w')
for k,v in hosts.items():
	mf.write(str(k)+' '+str(v["start"])+' '+str(v["finish"])+'\n')
mf.close()


